<?php

class rooms_has_discounts_model extends MY_Model {

    function __construct() {
        parent::__construct();
        $this->table = 'rooms_has_discounts';
    }

    // override
    public function find_all($sort = 'ID', $order = 'ASC') {
        $query = $this->db->get($this->table);
        return $query->result_array();
    }

    public function delete_by_room($room_id) {

        $q = "DELETE FROM rooms_has_discounts WHERE rooms_ID = ?";
        $this->db->query($q, array($room_id));
    }

    public function room_have_discount($room_id) {
        $q = "SELECT COUNT(*) as nr FROM rooms_has_discounts WHERE rooms_ID = ?";
        $res = $this->db->query($q, array((int) $room_id))->row_array();
        return $res['nr'];
    }

    public function find_discount_id($room_id) {
        $q = "SELECT discounts_ID FROM rooms_has_discounts WHERE rooms_ID = ?";
        $res = $this->db->query($q, array((int) $room_id))->row_array();
        if (isset($res['discounts_ID']))
            return $res['discounts_ID'];
        else
            return 0;
    }

    public function object_have_discount($object_id, $rooms) {

        if (count($rooms) == 0)
            return 0;

        $rooms_id = array();
        foreach ($rooms as $room):
            array_push($rooms_id, $room["ID"]);
        endforeach;

        if (count($rooms_id) > 0) {
            $rooms_id = implode(',', $rooms_id);
            $q = "SELECT discounts_ID FROM rooms_has_discounts WHERE rooms_ID IN ($rooms_id)";
            $res = $this->db->query($q)->row_array();
            if (isset($res['discounts_ID']))
                return $res['discounts_ID'];
            else
                return 0;
        }else
            return 0;
    }

}

?>
